#include <stdio.h>
#include <malloc.h>
#include <assert.h>


//动态数据表
typedef struct {
    int *arr;
    int size;
    int capacity;//空间容量
}SL;
void init(SL *p){
    p->arr=NULL;
    p->size=0;
    p->capacity=0;
}
//销毁动态开辟的内存空间
void destory(SL *p){
    free(p->arr);
    p->arr=NULL;
    p->size=0;
    p->capacity=0;
}
//扩容
void checkCapacity(SL *p){
    if(p->size==p->capacity){
        //申请空间
        int newCapacity=p->capacity==0?4 : 2*p->capacity;
        int *temp= realloc(p->arr,sizeof (int)*newCapacity);
        if(temp==NULL){
            printf("扩容失败");
            exit(1);
        }
        p->arr=temp;
        p->capacity=newCapacity;
        printf("%d",p->capacity);
    }
}
//打印数组
void SLprintf (SL *p){
    for (int i = 0; i < p->size; ++i) {
        printf("%d\n",p->arr[i]);
    }
}
//尾部插入元素
void pushBack(SL *p,int n){
    checkCapacity(p);
    p->arr[p->size++]=n;
}
void pushFront(SL *p,int n){
    checkCapacity(p);
    for (int i = p->size; i >0 ; i--) {
        p->arr[i]=p->arr[i-1];
    }
    p->arr[0]=n;
    p->size++;
}
void insert(SL *p,int pos,int n){
    if(pos>=0&&pos<=p->size){
        checkCapacity(p);
        for (int i = p->size; i >pos ; i--) {
            p->arr[i]=p->arr[i-1];
        }
        p->arr[pos]=n;
        p->size++;
    }else{
        printf("插入的元素应该在顺序表的范围内");
    }
}
void deleteFront(SL *p){
    for (int i = 0; i < p->size ;++i) {
        p->arr[i]=p->arr[i+1];
    }
    p->size--;
}
void delete(SL *p,int pos){
    assert(pos>=0&&pos<=p->size);
    for (int i = pos; i <p->size ; ++i) {
        p->arr[i] = p->arr[i + 1];
    }
    p->size--;
}
int main() {
    SL L;
    init(&L);
    pushBack(&L,15);
    pushFront(&L,12);
    insert(&L,2,23);
    SLprintf(&L);
    return 0;

}
